package edu.ucdavis.gwt.gis.client.export;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.ucdavis.gwt.gis.client.export.ImageExporter.ImageExportHandler;
import edu.ucdavis.gwt.gis.client.resources.GadgetResources;
import edu.ucdavis.gwt.gis.client.toolbar.Toolbar;


public class ToolbarImageExporter {

	private DownloadPopup popup = new DownloadPopup();
	private Toolbar toolbar;
	private Image icon = new Image(GadgetResources.INSTANCE.exportMap());
	
	public void export() {
		ImageExporter.INSTANCE.export(handler);
	}
	
	private void setError(String msg) {
		icon.setResource(GadgetResources.INSTANCE.exportMap());
		setStatusMessage(new HTML("<span style='white-space: nowrap;'>"+msg+"</span>"));
	}
	
	private void setSuccess(Image image) {
		popup.showImage(image);
		setStatusMessage(new HTML(""));
	}
	
	private ImageExportHandler handler = new ImageExportHandler() {
		@Override
		public void onComplete(Image img) {
			setSuccess(img);
		}
		@Override
		public void onFailure(String message) {
			setError(message);
		}
		@Override
		public void onStart() {
			setStatusMessage(new HTML("<span style='white-space:nowrap'>&nbsp;Flattening Map...&nbsp;</span>"));
		}
	};
	
	public void setToolbar(Toolbar toolbar) {
		this.toolbar = toolbar;
	}
	
	private void setStatusMessage(HTML message) {
		if( message.getHTML().length() > 0 ) {
			toolbar.setMessage(message);
		} else {
			toolbar.setMessage(new HTML(""));
		}
	}
	
	public Image getIcon() {
		return icon;
	}
	
	private class DownloadPopup extends PopupPanel {
		
		private SimplePanel imagePanel = new SimplePanel();
		
		public DownloadPopup() {
			setAnimationEnabled(true);
			setStyleName("exportPanel");
			
			HorizontalPanel hp = new HorizontalPanel();
			
			VerticalPanel vp = new VerticalPanel();
			vp.setWidth("100%");
			
			HTML directions = new HTML("<div style='padding: 20px; color:white; font-size: 18px'>Right-Click Image To Save</div>");
			vp.add(directions);
			vp.setCellHorizontalAlignment(directions, HorizontalPanel.ALIGN_CENTER);
			
			HTML close = new HTML("<div style='padding-bottom: 10px; color:white; font-size: 16px; text-decoration: underline; cursor:pointer'>Close</div>");
			close.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					hide();
				}
			});
			vp.add(close);
			vp.setCellHorizontalAlignment(close, HorizontalPanel.ALIGN_CENTER);
			
			hp.add(imagePanel);
			hp.setCellHorizontalAlignment(imagePanel, HorizontalPanel.ALIGN_CENTER);
			
			hp.add(vp);
			
			setWidget(hp);
		}
		
		public void showImage(Image image) {
			image.setVisible(true);
			image.removeFromParent();
			imagePanel.clear();
			imagePanel.add(image);
			image.setStyleName("MapExportImage");
			
			setWidth(Window.getClientWidth()+"px");
			setHeight(Window.getClientHeight()+"px");
			
			center();
			show();
		}
		
	}
	
}
